import 'package:flutter/material.dart';
import 'dart:math' as math;

/// 手绘模块图标类型枚举
enum HandDrawnModuleIconType {
  diary,      // 情绪日记
  test,       // 心理测试
  meditation, // 正念训练
  crisis,      // 危机干预
  thought,    // 思维训练
}

/// 手绘模块图标绘制类
class HandDrawnModuleIcon extends StatelessWidget {
  final HandDrawnModuleIconType type;
  final double size;
  final Color color;

  const HandDrawnModuleIcon({
    Key? key,
    required this.type,
    required this.size,
    required this.color,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return CustomPaint(
      size: Size(size, size),
      painter: _HandDrawnModuleIconPainter(
        type: type,
        color: color,
      ),
    );
  }
}

/// 手绘模块图标画笔类
class _HandDrawnModuleIconPainter extends CustomPainter {
  final HandDrawnModuleIconType type;
  final Color color;

  _HandDrawnModuleIconPainter({
    required this.type,
    required this.color,
  });

  @override
  void paint(Canvas canvas, Size size) {
    final paint = Paint()
      ..color = color
      ..style = PaintingStyle.stroke
      ..strokeWidth = 2.0
      ..strokeCap = StrokeCap.round;

    final center = Offset(size.width / 2, size.height / 2);
    final radius = size.width * 0.4;

    switch (type) {
      case HandDrawnModuleIconType.diary:
        _drawDiaryIcon(canvas, size, paint, center, radius);
        break;
      case HandDrawnModuleIconType.test:
        _drawTestIcon(canvas, size, paint, center, radius);
        break;
      case HandDrawnModuleIconType.meditation:
        _drawMeditationIcon(canvas, size, paint, center, radius);
        break;
      case HandDrawnModuleIconType.crisis:
        _drawCrisisIcon(canvas, size, paint, center, radius);
        break;
      case HandDrawnModuleIconType.thought:
        _drawThoughtIcon(canvas, size, paint);
        break;
    }
  }

  /// 绘制情绪日记图标（手绘风格的书本和心形）
  void _drawDiaryIcon(Canvas canvas, Size size, Paint paint, Offset center, double radius) {
    // 绘制书本外形
    final bookPath = Path();
    final bookLeft = center.dx - radius;
    final bookRight = center.dx + radius;
    final bookTop = center.dy - radius;
    final bookBottom = center.dy + radius;

    // 绘制书本封面（带有手绘风格的不规则线条）
    bookPath.moveTo(bookLeft, bookTop);
    bookPath.quadraticBezierTo(
      bookLeft + radius * 0.2, bookTop + radius * 0.1,
      bookLeft, bookBottom
    );
    bookPath.quadraticBezierTo(
      center.dx, bookBottom + radius * 0.1,
      bookRight, bookBottom
    );
    bookPath.quadraticBezierTo(
      bookRight - radius * 0.2, bookTop + radius * 0.1,
      bookRight, bookTop
    );
    bookPath.quadraticBezierTo(
      center.dx, bookTop - radius * 0.1,
      bookLeft, bookTop
    );

    // 绘制书页线条
    for (var i = 1; i <= 3; i++) {
      final y = bookTop + (bookBottom - bookTop) * i / 4;
      final path = Path();
      path.moveTo(bookLeft + radius * 0.3, y);
      path.quadraticBezierTo(
        center.dx, y + radius * 0.1,
        bookRight - radius * 0.3, y
      );
      canvas.drawPath(path, paint);
    }

    canvas.drawPath(bookPath, paint);
  }

  /// 绘制心理测试图标（手绘风格的问号和思考气泡）
  void _drawTestIcon(Canvas canvas, Size size, Paint paint, Offset center, double radius) {
    // 绘制思考气泡
    final bubblePath = Path();
    bubblePath.moveTo(center.dx - radius, center.dy);
    
    // 绘制气泡主体
    for (var i = 0; i < 8; i++) {
      final angle = i * math.pi / 4;
      final nextAngle = (i + 1) * math.pi / 4;
      final point = Offset(
        center.dx + math.cos(angle) * radius,
        center.dy + math.sin(angle) * radius
      );
      final nextPoint = Offset(
        center.dx + math.cos(nextAngle) * radius,
        center.dy + math.sin(nextAngle) * radius
      );
      final controlPoint = Offset(
        center.dx + math.cos((angle + nextAngle) / 2) * radius * 1.2,
        center.dy + math.sin((angle + nextAngle) / 2) * radius * 1.2
      );
      
      if (i == 0) {
        bubblePath.moveTo(point.dx, point.dy);
      }
      bubblePath.quadraticBezierTo(controlPoint.dx, controlPoint.dy, nextPoint.dx, nextPoint.dy);
    }

    // 绘制问号
    final questionPath = Path();
    // 问号的弧线部分
    questionPath.moveTo(center.dx - radius * 0.3, center.dy - radius * 0.2);
    questionPath.quadraticBezierTo(
      center.dx - radius * 0.3, center.dy - radius * 0.4,
      center.dx, center.dy - radius * 0.4
    );
    questionPath.quadraticBezierTo(
      center.dx + radius * 0.3, center.dy - radius * 0.4,
      center.dx + radius * 0.3, center.dy - radius * 0.2
    );
    questionPath.quadraticBezierTo(
      center.dx + radius * 0.3, center.dy,
      center.dx, center.dy
    );
    // 问号的点
    canvas.drawCircle(Offset(center.dx, center.dy + radius * 0.3), radius * 0.1, paint);

    canvas.drawPath(bubblePath, paint);
    canvas.drawPath(questionPath, paint);
  }

  /// 绘制正念训练图标（手绘风格的莲花）
  void _drawMeditationIcon(Canvas canvas, Size size, Paint paint, Offset center, double radius) {
    // 绘制莲花
    final petalCount = 8;
    final petalPath = Path();
    
    // 绘制花瓣
    for (var i = 0; i < petalCount; i++) {
      final angle = i * 2 * math.pi / petalCount;
      final startPoint = Offset(
        center.dx + math.cos(angle) * radius * 0.3,
        center.dy + math.sin(angle) * radius * 0.3
      );
      final endPoint = Offset(
        center.dx + math.cos(angle) * radius,
        center.dy + math.sin(angle) * radius
      );
      final controlPoint1 = Offset(
        center.dx + math.cos(angle - 0.3) * radius * 0.8,
        center.dy + math.sin(angle - 0.3) * radius * 0.8
      );
      final controlPoint2 = Offset(
        center.dx + math.cos(angle + 0.3) * radius * 0.8,
        center.dy + math.sin(angle + 0.3) * radius * 0.8
      );

      petalPath.moveTo(startPoint.dx, startPoint.dy);
      petalPath.cubicTo(
        controlPoint1.dx, controlPoint1.dy,
        controlPoint2.dx, controlPoint2.dy,
        endPoint.dx, endPoint.dy
      );
      petalPath.cubicTo(
        controlPoint2.dx, controlPoint2.dy,
        controlPoint1.dx, controlPoint1.dy,
        startPoint.dx, startPoint.dy
      );
    }

    // 绘制花蕊
    canvas.drawCircle(center, radius * 0.3, paint);
    canvas.drawPath(petalPath, paint);
  }

  /// 绘制危机干预图标（手绘风格的心跳线和十字）
  void _drawCrisisIcon(Canvas canvas, Size size, Paint paint, Offset center, double radius) {
    // 绘制心跳线
    final heartbeatPath = Path();
    final startX = center.dx - radius;
    final endX = center.dx + radius;
    final baseY = center.dy;

    // 起始平线
    heartbeatPath.moveTo(startX, baseY);
    heartbeatPath.lineTo(center.dx - radius * 0.6, baseY);

    // 心跳波峰
    heartbeatPath.lineTo(center.dx - radius * 0.4, baseY - radius * 0.6);
    heartbeatPath.lineTo(center.dx - radius * 0.2, baseY + radius * 0.6);
    heartbeatPath.lineTo(center.dx + radius * 0.2, baseY - radius * 0.3);

    // 结束平线
    heartbeatPath.lineTo(center.dx + radius * 0.6, baseY);
    heartbeatPath.lineTo(endX, baseY);

    // 绘制十字
    final crossPath = Path();
    final crossSize = radius * 0.4;
    // 垂直线
    crossPath.moveTo(center.dx, center.dy - crossSize);
    crossPath.lineTo(center.dx, center.dy + crossSize);
    // 水平线
    crossPath.moveTo(center.dx - crossSize, center.dy);
    crossPath.lineTo(center.dx + crossSize, center.dy);

    // 绘制路径
    canvas.drawPath(heartbeatPath, paint);
    canvas.save();
    canvas.translate(radius * 0.8, -radius * 0.8);
    canvas.drawPath(crossPath, paint);
    canvas.restore();
  }

  void _drawThoughtIcon(Canvas canvas, Size size, Paint paint) {
    final path = Path();
    final center = Offset(size.width / 2, size.height / 2);
    final radius = size.width * 0.3;

    // 绘制主要思维泡泡
    path.addOval(Rect.fromCircle(center: center, radius: radius));

    // 绘制小泡泡
    path.addOval(Rect.fromCircle(
      center: Offset(center.dx - radius * 0.8, center.dy + radius * 0.8),
      radius: radius * 0.4,
    ));
    path.addOval(Rect.fromCircle(
      center: Offset(center.dx - radius * 1.2, center.dy + radius * 1.2),
      radius: radius * 0.2,
    ));

    // 在主泡泡内绘制闪电符号（代表思维转变）
    final lightning = Path();
    final top = center.dy - radius * 0.5;
    final bottom = center.dy + radius * 0.5;
    final left = center.dx - radius * 0.3;
    final right = center.dx + radius * 0.3;

    lightning.moveTo(left, top);
    lightning.lineTo(center.dx, center.dy - radius * 0.1);
    lightning.lineTo(center.dx - radius * 0.2, center.dy);
    lightning.lineTo(right, bottom);

    canvas.drawPath(path, paint);
    canvas.drawPath(lightning, paint);
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) => false;
} 